package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pojo.News;
import utils.PageUtil;
import utils.PreHandle;

public class NewsDao {

	private static final int PAGESIZE = 10;

	public static PageUtil<News> doPageSearch(Integer pageNo, Integer ntid) {

		List<Object> parm = new ArrayList<Object>();

		List<Object> parm1 = new ArrayList<Object>();

		PageUtil<News> pageutil = new PageUtil<News>();

		int start = (pageNo - 1) * PAGESIZE;

		String sql = "SELECT * FROM news Where 1=1 ";

		String sql1 = "select count(1) from news where 1=1";

		if (ntid != null) {
			sql += " and ntid=?";
			sql1 += " and ntid=?";
			parm.add(ntid);
			parm1.add(ntid);
		}
		parm.add(start);
		parm.add(PAGESIZE);
		sql += " ORDER BY ncreateDate DESC LIMIT ?,?";

		sql1 += " ORDER BY ncreateDate ";

		System.out.println(sql + parm.toArray());

		System.out.println(sql1 + parm.toArray());

		Connection con = PreHandle.getConnection();

		PreparedStatement pstmt = null;

		ResultSet res = null;

		try {
			pstmt = con.prepareStatement(sql1);

			for (int i = 0, index = 1; i < parm1.size(); i++) {
				pstmt.setInt(index++, (int) parm.get(i));
			}
			// System.out.println("conncetion class:" +
			// con.getClass().getName()+con.hashCode());
			res = pstmt.executeQuery();

			if (res.next())

				pageutil.setRows(res.getInt(1));

			pageutil.setNews(PreHandle.query(sql, News.class, parm.toArray()));

			pageutil.setStart(start);

			pageutil.setPage(pageNo);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			PreHandle.closeAll(res, pstmt, con);
		}

		return pageutil;

	}

	public static PageUtil<News> doPageSearch1(Integer pageNo) {

		PageUtil<News> pageutil = new PageUtil<News>();

		int start = (pageNo - 1) * PAGESIZE;

		String sql = "SELECT * FROM news ORDER BY ncreateDate DESC LIMIT ?,? ";

		String sql1 = "select count(1) from news ";

		Connection con = PreHandle.getConnection();

		PreparedStatement pstmt = null;

		ResultSet res = null;

		try {
			pstmt = con.prepareStatement(sql1);

			// System.out.println("conncetion class:" +
			// con.getClass().getName()+con.hashCode());
			res = pstmt.executeQuery();

			if (res.next())

				pageutil.setRows(res.getInt(1));

			pageutil.setNews(PreHandle.query(sql, News.class, start, PAGESIZE));

			pageutil.setStart(start);

			pageutil.setPage(pageNo);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			PreHandle.closeAll(res, pstmt, con);
		}

		return pageutil;

	}

	public static News doSearchById(Integer nid) {

		String sql = "select * from news where nid=?";

		List<News> list = PreHandle.query(sql, News.class, nid);

		News news = list.get(0);

		return news;

	}

	public static Boolean addNews(News news) {

		boolean isOk = false;

		String sql = "insert into news (ntitle,nauthor,npicPath,ncontent,nsummary,ncreateDate,nmodifyDate,ntid)"
				+ "values(?,?,?,?,?,?,?,?)";

		try {

			isOk = PreHandle.excuteUpdate(sql, news) == 1;

		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return isOk;

	}

	public static Boolean deleteNews(Integer nid) {

		boolean isOk = false;

		String sql = "delete from news where nid=?";

		try {

			isOk = PreHandle.excuteUpdate(sql, nid) == 1;

		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return isOk;

	}

	public static Boolean updateNews(News news) {

		boolean isOk = false;

		String sql = "update news set ntitle=?,nauthor=?,npicPath=?,ncontent=?,nsummary=?,"
				+ "ncreateDate=?,nmodifyDate=?,ntid=? where nid=?";

		try {

			isOk = PreHandle.excuteUpdate(sql, news) == 1;

		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(sql);
		return isOk;
	}

	public static List<News> doSearchTopX(Integer ntid) {

		String sql = "select * from news where ntid=? order by ncreateDate desc limit 0,5";

		return PreHandle.query(sql, News.class, ntid);

	}

	// public static void main(String[] args) {
	// NewsDao news = new NewsDao();
	//
	// PageUtil<News> pageUtil = news.doPageSearch(1);
	//
	//
	// List<News> list = pageUtil.getNews();
	//
	// System.out.println(">>>>" + list.size());
	//
	// System.out.println(">>>>" + pageUtil.getRows());
	//
	// System.out.println(pageUtil.getRows()/list.size()+(pageUtil.getRows()%list.size()==0?0:1));
	//
	// System.out.println(pageUtil.getTotalPage() + ">>>>>>>>>>>");
	//
	// System.out.println(pageUtil.getPage()+" "+pageUtil.getRows()+"
	// "+pageUtil.getStart()+" "+pageUtil.getTotalPage());
	// }
}
